/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sigodonto.model.dao;

import br.com.sigodonto.model.entity.Dentista;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Fidelis
 */
public class DentistaDao extends DaoGenerico implements Serializable{

    public DentistaDao() {
        super();
    }
    
    public Dentista verificarLogin(String username, String senha) {
        Dentista usuario;
        this.getEntityManager().getTransaction().begin();

        try {
            Query q = this.getEntityManager().createQuery("select d from Dentista d where d.contaUsuario.username = :username and d.contaUsuario.senha = :senha");
            q.setParameter("username", username);
            q.setParameter("senha", senha);
            usuario = (Dentista) q.getSingleResult();
            this.getEntityManager().getTransaction().commit();
            return usuario;
        } catch (Exception ex) {
            this.getEntityManager().getTransaction().rollback();
            return null;
        }
    }
    
    public boolean temUsername(String username) {
        this.getEntityManager().getTransaction().begin();

        try {
            Query q = this.getEntityManager().createQuery("select d from Dentista d where d.ContaUsuario.username = :username");
            q.setParameter("username", username);
            List list = q.getResultList();
            this.getEntityManager().getTransaction().commit();
            if(list.isEmpty()){
                return false;
            }else{
                return true;
            }
        } catch (Exception ex) {
            this.getEntityManager().getTransaction().rollback();
            return false;
        }
    }
    
}
